package com.yunshipei.enterplorer.browser;

import android.content.Context;
import android.util.AttributeSet;

import com.yunshipei.model.YspEvent.ToolbarModelEvent;

import org.xwalk.core.XWalkResourceClient;
import org.xwalk.core.XWalkUIClient;
import org.xwalk.core.XWalkView;

import io.rong.eventbus.EventBus;

public class HomeWebView extends XWalkView {

    private int upscrollHeight = 0;
    private int downscrollHeight = 0;
    private boolean isShowing = true;
    private int mToolbarHeight;

    public HomeWebView(Context context) {
        super(context);
    }

    public HomeWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        if (t > oldt) {
            downscrollHeight = 0;
            upscrollHeight += t - oldt;
            if (upscrollHeight > mToolbarHeight && isShowing) {
                isShowing = false;
                EventBus.getDefault().post(new ToolbarModelEvent(false));
            }
        } else if (oldt > t) {
            upscrollHeight = 0;
            downscrollHeight += oldt - t;
            if (downscrollHeight > 30 && !isShowing) {
                isShowing = true;
                EventBus.getDefault().post(new ToolbarModelEvent(true));
            }
        }
    }

    public void setToolbarHeight(int height) {
        this.mToolbarHeight = height;
    }

    public void setWebViewClient(XWalkResourceClient webViewClient) {
        setResourceClient(webViewClient);
    }

    public void setWebChromeClient(XWalkUIClient webChromeClient) {
        setUIClient(webChromeClient);
    }

    public void loadUrl(String mHomeURL) {
        load(mHomeURL, null);
    }

    public void reload() {
        reload(0);
    }

}
